package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

public class StudentServiceImpl implements StudentService {
    /**
     * 分页查询方法
     * @param currentPage 当前页数
     * @param pageSize 每页展示条数
     * @return
     */

    public PageInfo<Student> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //设置分页数据（当前页和每页展示的条数）
        PageHelper.startPage(currentPage, pageSize);
        //执行操作
        Page<Student> page = mapper.findAll();
        //封装分页信息
        PageInfo<Student> info = new PageInfo<Student>(page);
        sqlSession.commit();
        sqlSession.close();
        return info;
    }

    /**
     * 添加学生信息
     * @param student
     * @return
     */

    public int addStudent(Student student) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.addStudent(student);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    /**
     * 修改学生的方法
     * @param student
     * @return
     */

    public int updateStudent(Student student) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.updateStudent(student);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    /**
     * 删除学生的方法
     * @param number
     * @return
     */

    public int deleteStudent(String number) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.deleteStudent(number);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
}
